
/*
    Copyright (C) 1999-2008 by Mark D. Hill and David A. Wood for the
    Wisconsin Multifacet Project.  Contact: gems@cs.wisc.edu
    http://www.cs.wisc.edu/gems/

    --------------------------------------------------------------------

    This file is part of the SLICC (Specification Language for 
    Implementing Cache Coherence), a component of the Multifacet GEMS 
    (General Execution-driven Multiprocessor Simulator) software 
    toolset originally developed at the University of Wisconsin-Madison.

    SLICC was originally developed by Milo Martin with substantial
    contributions from Daniel Sorin.

    Substantial further development of Multifacet GEMS at the
    University of Wisconsin was performed by Alaa Alameldeen, Brad
    Beckmann, Jayaram Bobba, Ross Dickson, Dan Gibson, Pacia Harper,
    Derek Hower, Milo Martin, Michael Marty, Carl Mauer, Michelle Moravan,
    Kevin Moore, Andrew Phelps, Manoj Plakal, Daniel Sorin, Haris Volos, 
    Min Xu, and Luke Yen.
    --------------------------------------------------------------------

    If your use of this software contributes to a published paper, we
    request that you (1) cite our summary paper that appears on our
    website (http://www.cs.wisc.edu/gems/) and (2) e-mail a citation
    for your published paper to gems@cs.wisc.edu.

    If you redistribute derivatives of this software, we request that
    you notify us and either (1) ask people to register with us at our
    website (http://www.cs.wisc.edu/gems/) or (2) collect registration
    information and periodically send it to us.

    --------------------------------------------------------------------

    Multifacet GEMS is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    Multifacet GEMS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with the Multifacet GEMS; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307, USA

    The GNU General Public License is contained in the file LICENSE.

### END HEADER ###
*/

/*
 * $Id$
 *
 */

#include "mif_gen.h"
#include "State.h"
#include "Event.h"
#include "Action.h"
#include "Transition.h"

// -- Helper functions
string formatShorthand(const string shorthand);
string formatCellRuling(const string shorthand);

void printStateTableMIF(const StateMachine& sm, ostream& out)
{
 const string mif_prolog1 = 
"<MIFFile 5.50> # Generated by Multifacet MIF Mungers Inc\n\
<Tbls\n\
 <Tbl\n\
  <TblID 1>\n\
  <TblTag `Format A'>\n\
  <TblFormat\n\
\n\
   <TblAlignment Center>\n\
\n\
   # <TblXColumnNum 0>\n\
   <TblXColumnRuling `Medium'>\n\
\n\
   <TblLRuling `Medium'>\n\
   <TblRRuling `Medium'>\n\
   <TblTRuling `Medium'>\n\
   <TblBRuling `Medium'>\n\
\n\
   <TblColumn\n\
    <TblColumnNum 0>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
   <TblColumn\n\
    <TblColumnNum 1>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
  > # end of TblFormat\n\
\n\
  <TblNumColumns 2>\n\
  <TblColumnWidth  0.51\">\n\
  <TblColumnWidth  6.00\">\n\
  <TblTitle\n\
   <TblTitleContent\n\
    <Para\n\
     <PgfTag `TableTitle'>\n\
     <Pgf\n\
      <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
     > # end of Pgf\n\
     <PgfNumString `TABLE 1. '>\n\
     <ParaLine\n\
      <Marker\n\
       <MType 9>\n\
       <MTypeName `Cross-Ref'>\n\
       <MCurrPage `1'>\n\
      > # end of Marker\n\
      <String `";

 const string mif_prolog2 =
   "'>\n\
     > # end of ParaLine\n\
    > # end of Para\n\
   > # end of TblTitleContent\n\
  > # end of TblTitle\n\
\n\
  <TblH\n\
   <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.44444\">\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
        <String `State'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       <ParaLine\n\
        <String `Description'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
  > # end of TblH\n\
\n\
  <TblBody\n\
";

 const string row_before_state = 
" <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.22222\">\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
         <String `";
 
 const string row_between_state_desc = 
         "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <ParaLine\n\
        <String `";

 const string row_after_desc = 
        "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
";

 const string mif_epilog = 
"  > # end of TblBody\n\
 > # end of Tbl\n\
> # end of Tbls\n\
\n\
 <Para\n\
  <ParaLine\n\
   <ATbl 1>\n\
  > # end of ParaLine\n\
 > # end of Para\n\
\n\
# End of MIFFile\n\
";
 
 out << mif_prolog1;
 out << formatShorthand( sm.getShorthand() );
 out << " states";
 out << mif_prolog2;
 
 for( int i = 0; i < sm.numStates(); i++ )
 {
   out << row_before_state;
   out << formatShorthand( sm.getState( i ).getShorthand() );
   out << row_between_state_desc;
   out << sm.getState( i ).getDescription();
   out << row_after_desc;
 }
 
 out << mif_epilog;
}


void printEventTableMIF(const StateMachine& sm, ostream& out)
{
 const string mif_prolog1 = 
"<MIFFile 5.50> # Generated by Multifacet MIF Mungers Inc\n\
<Tbls\n\
 <Tbl\n\
  <TblID 1>\n\
  <TblTag `Format A'>\n\
  <TblFormat\n\
\n\
   <TblAlignment Center>\n\
\n\
   # <TblXColumnNum 0>\n\
   <TblXColumnRuling `Medium'>\n\
\n\
   <TblLRuling `Medium'>\n\
   <TblRRuling `Medium'>\n\
   <TblTRuling `Medium'>\n\
   <TblBRuling `Medium'>\n\
\n\
   <TblColumn\n\
    <TblColumnNum 0>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
   <TblColumn\n\
    <TblColumnNum 1>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
  > # end of TblFormat\n\
\n\
  <TblNumColumns 2>\n\
  <TblColumnWidth  0.51\">\n\
  <TblColumnWidth  6.00\">\n\
  <TblTitle\n\
   <TblTitleContent\n\
    <Para\n\
     <PgfTag `TableTitle'>\n\
     <Pgf\n\
      <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
     > # end of Pgf\n\
     <PgfNumString `TABLE 1. '>\n\
     <ParaLine\n\
      <Marker\n\
       <MType 9>\n\
       <MTypeName `Cross-Ref'>\n\
       <MCurrPage `1'>\n\
      > # end of Marker\n\
      <String `";
 const string mif_prolog2 = 
"'>\n\
     > # end of ParaLine\n\
    > # end of Para\n\
   > # end of TblTitleContent\n\
  > # end of TblTitle\n\
\n\
  <TblH\n\
   <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.44444\">\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
        <String `Event'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
       <ParaLine\n\
        <String `Description'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
  > # end of TblH\n\
\n\
  <TblBody\n\
";

 const string row_before_event = 
" <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.22222\">\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
         <String `";
 
 const string row_between_event_desc = 
         "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <ParaLine\n\
        <String `";

 const string row_after_desc = 
        "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
";

 const string mif_epilog = 
"  > # end of TblBody\n\
 > # end of Tbl\n\
> # end of Tbls\n\
\n\
 <Para\n\
  <ParaLine\n\
   <ATbl 1>\n\
  > # end of ParaLine\n\
 > # end of Para\n\
\n\
# End of MIFFile\n\
";
 
 out << mif_prolog1;
 out << formatShorthand( sm.getShorthand() );
 out << " events"; 
 out << mif_prolog2;
 
 for( int i = 0; i < sm.numEvents(); i++ )
 {
   out << row_before_event;
   out << formatShorthand( sm.getEvent( i ).getShorthand() );
   out << row_between_event_desc;
   out << sm.getEvent( i ).getDescription();
   out << row_after_desc;
 }
 
 out << mif_epilog;
}


void printActionTableMIF(const StateMachine& sm, ostream& out)
{
 const string mif_prolog1 = 
"<MIFFile 5.50> # Generated by Multifacet MIF Mungers Inc\n\
<Tbls\n\
 <Tbl\n\
  <TblID 1>\n\
  <TblTag `Format A'>\n\
  <TblFormat\n\
\n\
   <TblAlignment Center>\n\
\n\
   # <TblXColumnNum 0>\n\
   <TblXColumnRuling `Medium'>\n\
\n\
   <TblLRuling `Medium'>\n\
   <TblRRuling `Medium'>\n\
   <TblTRuling `Medium'>\n\
   <TblBRuling `Medium'>\n\
\n\
   <TblColumn\n\
    <TblColumnNum 0>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
   <TblColumn\n\
    <TblColumnNum 1>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
  > # end of TblFormat\n\
\n\
  <TblNumColumns 2>\n\
  <TblColumnWidth  0.51\">\n\
  <TblColumnWidth  6.00\">\n\
  <TblTitle\n\
   <TblTitleContent\n\
    <Para\n\
     <PgfTag `TableTitle'>\n\
     <Pgf\n\
      <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
     > # end of Pgf\n\
     <PgfNumString `TABLE 1. '>\n\
     <ParaLine\n\
      <Marker\n\
       <MType 9>\n\
       <MTypeName `Cross-Ref'>\n\
       <MCurrPage `1'>\n\
      > # end of Marker\n\
      <String `";
 const string mif_prolog2 = 
"'>\n\
     > # end of ParaLine\n\
    > # end of Para\n\
   > # end of TblTitleContent\n\
  > # end of TblTitle\n\
\n\
  <TblH\n\
   <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.44444\">\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
        <String `Action'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
       <ParaLine\n\
        <String `Description'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
  > # end of TblH\n\
\n\
  <TblBody\n\
";

 const string row_before_action = 
" <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.22222\">\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
         <String `";
 
 const string row_between_action_desc = 
         "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <ParaLine\n\
        <String `";

 const string row_after_desc = 
        "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
";

 const string mif_epilog = 
"  > # end of TblBody\n\
 > # end of Tbl\n\
> # end of Tbls\n\
\n\
 <Para\n\
  <ParaLine\n\
   <ATbl 1>\n\
  > # end of ParaLine\n\
 > # end of Para\n\
\n\
# End of MIFFile\n\
";
 
 out << mif_prolog1;
 out << formatShorthand( sm.getShorthand() );
 out << " actions";
 out << mif_prolog2;
 
 for( int i = 0; i < sm.numActions(); i++ )
 {
   out << row_before_action;
   out << formatShorthand( sm.getAction( i ).getShorthand() );
   out << row_between_action_desc;
   out << sm.getAction( i ).getDescription();
   out << row_after_desc;
 }
 
 out << mif_epilog;
}


void printTransitionTableMIF(const StateMachine& sm, ostream& out)
{
  const string mif_prolog = 
"<MIFFile 5.50> # Generated by Multifacet MIF Mungers Inc\n\
<Tbls\n\
 <Tbl\n\
  <TblID 1>\n\
  <TblTag `Format A'>\n\
  <TblFormat\n\
\n\
   <TblAlignment Center>\n\
\n\
   # <TblXColumnNum 0>\n\
   <TblXColumnRuling `Medium'>\n\
\n\
   <TblLRuling `Medium'>\n\
   <TblRRuling `Medium'>\n\
   <TblTRuling `Medium'>\n\
   <TblBRuling `Medium'>\n\
 \n\
";
  
  const string tbl_fmt_before_col_num =
"  <TblColumn\n\
    <TblColumnNum ";

  const string tbl_fmt_after_col_num =
                    ">\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
";
  
  const string tbl_fmt_before_num_cols = 
" > # end of TblFormat\n\
\n\
  <TblNumColumns ";
  
  const string tbl_fmt_each_col_width_begin =
                     ">\n\
   <TblColumnWidth  ";
  
  const string tbl_fmt_each_col_width_end = "\"";
  
  const string tbl_before_first_header1 = 
                      ">\n\
  <TblTitle\n\
   <TblTitleContent\n\
    <Para\n\
     <PgfTag `TableTitle'>\n\
     <Pgf\n\
      <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
     > # end of Pgf\n\
     <PgfNumString `TABLE 1. '>\n\
     <ParaLine\n\
      <Marker\n\
       <MType 9>\n\
       <MTypeName `Cross-Ref'>\n\
       <MCurrPage `1'>\n\
      > # end of Marker\n\
      <String `";

  const string tbl_before_first_header2 = 
    "'>\n\
     > # end of ParaLine\n\
    > # end of Para\n\
   > # end of TblTitleContent\n\
  > # end of TblTitle\n\
\n\
  <TblH\n\
   <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.44444\">";

  const string tbl_before_each_header = 
"   <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
         <String `";
  
  const string tbl_before_each_rot_header = 
"   <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellAngle 270>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
       <ParaLine\n\
         <String `";
  
  const string tbl_after_each_header = 
                  "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
";

  const string before_first_row = 
"   > # end of Row\n\
  > # end of TblH\n\
\n\
  <TblBody\n\
";
  
  const string row_before_first_cell = 
" <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.22222\">";
  
  const string row_cell_before_ruling =
"    <Cell\n\
";
  
  const string row_cell_before_contents =
"    <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
         <String `";

  const string row_cell_after_contents =
                  "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
";

  const string row_empty_cell = 
"    <CellFill 5>\n\
     <CellColor `Cyan'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <ParaLine\n\
         <String `'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
";

  const string row_after_last_cell =
"   > # end of Row\n\
";


  const string mif_epilog = 
"  > # end of TblBody\n\
 > # end of Tbl\n\
> # end of Tbls\n\
\n\
 <Para\n\
  <ParaLine\n\
   <ATbl 1>\n\
  > # end of ParaLine\n\
 > # end of Para\n\
\n\
# End of MIFFile\n\
";

  int i, j, num_rows, num_cols;
  string row_ruling;    
  string col_ruling;
  
  num_rows = sm.numStates();
  num_cols = sm.numEvents() + 1;
  
  // -- Prolog
  out << mif_prolog;
  
  // -- Table format (for each column)
  for( i = 0; i < num_cols; i++ )
  {
    out << tbl_fmt_before_col_num;
    out << i;
    out << tbl_fmt_after_col_num;
  }

  // -- Spell out width of each column

  // -- FIXME: make following constants into parameters
  const float total_table_width = 7.5;  // -- Total page width = 7.5" (portrait mode)
  const float min_col_width = 0.35;     // -- Min col width (for legibility)
  const float max_col_width = 0.75;     // -- Max col width (for aesthetics)
  float column_width;

  // -- Calculate column width and clamp it within a range
  column_width = total_table_width / num_cols;
  column_width = ((column_width < min_col_width) 
                   ? min_col_width
                   : ((column_width > max_col_width)
                      ? max_col_width
                      : column_width));

  out << tbl_fmt_before_num_cols;
  out << num_cols;
  for( i = 0; i < num_cols; i++ )
  {
    out << tbl_fmt_each_col_width_begin << column_width << tbl_fmt_each_col_width_end;
  }

  // -- Column headers
  out << tbl_before_first_header1;
  out << formatShorthand( sm.getShorthand() );
  out << " transitions";
  out << tbl_before_first_header2;

  out << tbl_before_each_header;
  out << "State";
  out << tbl_after_each_header;

  for( i = 0; i < sm.numEvents(); i++ )
  {
    out << tbl_before_each_rot_header;
    out << formatShorthand( sm.getEvent(i).getShorthand() );
    out << tbl_after_each_header;
  }
  out << before_first_row;


  // -- Body of table 
  for( i = 0; i < num_rows; i++ )
  {
    // -- Each row
    out << row_before_first_cell;

    // -- Figure out ruling
    if (sm.getState(i).existPair("format")) {
      row_ruling = formatCellRuling( sm.getState(i).lookupPair("format"));
    } else {
      row_ruling = "";
    }
    
    // -- First column = state
    out << row_cell_before_ruling;
    out << row_ruling;
    out << row_cell_before_contents;
    out << formatShorthand( sm.getState(i).getShorthand() );
    out << row_cell_after_contents;
    
    // -- One column for each event
    for( j = 0; j < sm.numEvents(); j++ )
    {
      const Transition* trans_ptr = sm.getTransPtr( i, j );

      // -- Figure out ruling
      if (sm.getEvent(j).existPair("format")) {
        col_ruling = formatCellRuling(sm.getEvent(j).lookupPair("format"));
      } else {
        col_ruling = "";
      }

      out << row_cell_before_ruling;
      out << row_ruling;
      out << col_ruling;
      
      if( trans_ptr != NULL )
      {
        string actions;
        string nextState;

        // -- Get the actions
        actions = formatShorthand( trans_ptr->getActionShorthands() );
        
        // -- Get the next state
        // FIXME: should compare index, not the string
        if (trans_ptr->getNextStateShorthand() != 
            sm.getState(i).getShorthand() ) 
        { 
          nextState = formatShorthand( trans_ptr->getNextStateShorthand() );
        } else 
        {
          nextState = "";
        }

        // -- Print out "actions/next-state"
        out << row_cell_before_contents;
        out << actions;
        if ((nextState.length() != 0) && (actions.length() != 0)) {
          out << "/";
        }
        out << nextState;
        out << row_cell_after_contents;
      }
      else
      {
        out << row_empty_cell;
      }

    }
    
    out << row_after_last_cell;
  }
  
  // -- Epilog
  out << mif_epilog;
  
}
/*
void printTBETableMIF(const StateMachine& sm, const Vector<Field>& fields, ostream& out)
{
 const string mif_prolog1 = 
"<MIFFile 5.50> # Generated by Multifacet MIF Mungers Inc\n\
<Tbls\n\
 <Tbl\n\
  <TblID 1>\n\
  <TblTag `Format A'>\n\
  <TblFormat\n\
\n\
   <TblAlignment Center>\n\
\n\
   # # <TblXColumnNum 0>\n\
   <TblXColumnRuling `Medium'>\n\
\n\
   <TblLRuling `Medium'>\n\
   <TblRRuling `Medium'>\n\
   <TblTRuling `Medium'>\n\
   <TblBRuling `Medium'>\n\
\n\
   <TblColumn\n\
    <TblColumnNum 0>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
   <TblColumn\n\
    <TblColumnNum 1>\n\
    <TblColumnWidth  0.51\">\n\
    <TblColumnH\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnH\n\
    <TblColumnBody\n\
     <PgfTag `CellBody'>\n\
    > # end of TblColumnBody\n\
    <TblColumnF\n\
     <PgfTag `CellHeading'>\n\
    > # end of TblColumnF\n\
   > # end of TblColumn\n\
  > # end of TblFormat\n\
\n\
  <TblNumColumns 2>\n\
  <TblColumnWidth  0.51\">\n\
  <TblColumnWidth  6.00\">\n\
  <TblTitle\n\
   <TblTitleContent\n\
    <Para\n\
     <PgfTag `TableTitle'>\n\
     <Pgf\n\
      <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
     > # end of Pgf\n\
     <PgfNumString `TABLE 1. '>\n\
     <ParaLine\n\
      <Marker\n\
       <MType 9>\n\
       <MTypeName `Cross-Ref'>\n\
       <MCurrPage `1'>\n\
      > # end of Marker\n\
      <String `";

 const string mif_prolog2 =
   "'>\n\
     > # end of ParaLine\n\
    > # end of Para\n\
   > # end of TblTitleContent\n\
  > # end of TblTitle\n\
\n\
  <TblH\n\
   <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.44444\">\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
        <String `Field'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellLRuling `Medium'>\n\
     <CellBRuling `Medium'>\n\
     <CellRRuling `Medium'>\n\
     <CellTRuling `Medium'>\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellHeading'>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       <ParaLine\n\
        <String `Description'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
  > # end of TblH\n\
\n\
  <TblBody\n\
";

 const string row_before_state = 
" <Row\n\
    <RowMaxHeight  14.0\">\n\
    <RowHeight  0.22222\">\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <Pgf\n\
        <PgfAlignment Center>\n\
        <PgfFont \n\
         <FTag `'>\n\
         <FFamily `Times'>\n\
         <FVar `Regular'>\n\
         <FWeight `Regular'>\n\
         <FAngle `Regular'>\n\
         <FPostScriptName `Times-Roman'>\n\
         <FEncoding `FrameRoman'>\n\
         <FSize  11.0 pt>\n\
         <FUnderlining FNoUnderlining>\n\
         <FOverline No>\n\
         <FStrike No>\n\
         <FChangeBar No>\n\
         <FOutline No>\n\
         <FShadow No>\n\
         <FPairKern Yes>\n\
         <FTsume No>\n\
         <FCase FAsTyped>\n\
         <FPosition FNormal>\n\
         <FDX  0.0%>\n\
         <FDY  0.0%>\n\
         <FDW  0.0%>\n\
         <FStretch  100.0%>\n\
         <FLanguage USEnglish>\n\
         <FLocked No>\n\
         <FSeparation 0>\n\
         <FColor `Black'>\n\
        > # end of PgfFont\n\
       >\n\
       <ParaLine\n\
         <String `";
 
 const string row_between_state_desc = 
         "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
    <Cell\n\
     <CellContent\n\
      <Para\n\
       <PgfTag `CellBody'>\n\
       <ParaLine\n\
        <String `";

 const string row_after_desc = 
        "'>\n\
       > # end of ParaLine\n\
      > # end of Para\n\
     > # end of CellContent\n\
    > # end of Cell\n\
   > # end of Row\n\
";

 const string mif_epilog = 
"  > # end of TblBody\n\
 > # end of Tbl\n\
> # end of Tbls\n\
\n\
 <Para\n\
  <ParaLine\n\
   <ATbl 1>\n\
  > # end of ParaLine\n\
 > # end of Para\n\
\n\
# End of MIFFile\n\
";
 
 out << mif_prolog1;
 out << sm.getShorthand();
 out << " TBE";
 out << mif_prolog2;
 
 for( int i = 0; i < fields.size(); i++ ) {
   out << row_before_state;
   out << formatShorthand(fields[i].getShorthand());
   out << row_between_state_desc;
   out << fields[i].getDescription();
   out << row_after_desc;
 }
 
 out << mif_epilog;
}
*/
// --
// -- Helper function to do some shorthand formatting (kludge before we
// -- get the tuple attributes into the state machine language.
// -- Current convention:
// --    - each `_' indicates a toggle between normal mode and superscript
// --    - each escaped (using `\') character indicates a letter formatted
// --      using the Symbol character set. \a = alpha, \b = beta, \c = chi etc.
// --      See the FrameMaker character sets manual in the Online Manuals.
// --    - a `!' indicates extra stuff at the end which can be ignored (used
// --      for determining cell ruling and so on)
// --
string formatShorthand(const string shorthand)
{
  string munged_shorthand = "";
  bool mode_is_normal = true;
  const string mif_superscript = "'> <Font <FPosition FSuperscript> <FLocked No> > <String `";
  const string mif_normal = "'> <Font <FPosition FNormal> <FLocked No> > <String `";
  const string mif_symbol = "'> <Font <FFamily `Symbol'> <FPostScriptName `Symbol'> <FEncoding `FrameRoman'> <FLocked No> > <String `";
  const string mif_times = "'> <Font <FFamily `Times'> <FPostScriptName `Times-Roman'> <FEncoding `FrameRoman'> <FLocked No> > <String `";
  

  // -- Walk over the string, processing superscript directives
  for( unsigned int i = 0; i < shorthand.length(); i++ )
  {
    if( shorthand[i] == '!' )
    {
      // -- Reached logical end of shorthand name
      break;
    }
    else if( shorthand[i] == '^' )
    {
      // -- Process super/subscript formatting

      mode_is_normal = !mode_is_normal;
      if( mode_is_normal )
      {
        // -- Back to normal mode
        munged_shorthand += mif_normal;
      }
      else
      {
        // -- Going to superscript mode
        munged_shorthand += mif_superscript;
      }
      
    }
    else if( shorthand[i] == '\\' )
    {
      // -- Process Symbol character set
      if( (i + 1) < shorthand.length() )
      {
        i++;  // -- Proceed to next char. Yes I know that changing the loop var is ugly!
        munged_shorthand += mif_symbol;
        munged_shorthand += shorthand[i];
        munged_shorthand += mif_times;
      }
      else
      {
        // -- FIXME: Add line number info later
        cerr << "Encountered a `\\` without anything following it!" << endl;
        exit( -1 );
      }
      
    }
    else
    {
      // -- Pass on un-munged
      munged_shorthand += shorthand[i];
    }
    
  } // -- end for all characters in shorthand

  // -- Do any other munging 

  // -- Return the formatted shorthand name
  return munged_shorthand;
}


// --
// -- Helper function to figure out where to put rules in the table (kludge before we
// -- get the tuple attributes into the shorthand machine language.
// -- Current convention:
// --    - a `!' in the shorthand indicates the beginning of ruling information
// --    - `b' => bottom of this row is ruled
// --    - `r' => right of this column is ruled
// --
string formatCellRuling( const string shorthand)
{
  for( unsigned int i = 0; i < shorthand.length(); i++ )
  {
    if( shorthand[i] == '!' )
    {
      // -- OK, found beginning of ruling information
      for( unsigned int j = i+1; j < shorthand.length(); j++ )
      {
        if( shorthand[j] == 'b')
        {
          // -- Rule the bottom
          return "<CellBRuling `Medium'>\n";
        }
        else if( shorthand[j] == 'r')
        {
          // -- Rule the bottom
          return "<CellRRuling `Medium'>\n";
        }
        
      }

      // -- No ruling directives recognized, return default ruling
      return "";
    }
    
  }
  
  // -- No ruling information found, return default ruling
  return "";
}
